package com.example.adopt.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adopt.entity.*;
import com.example.adopt.entity.vo.UserVo;
import com.example.adopt.service.PetService;
import com.example.adopt.service.ProductService;
import com.example.adopt.service.UserService;
import com.example.adopt.util.JWTUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjl
 * @since 2024-09-13
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    ObjectMapper objectMapper;
    @Autowired
    PetService petService;
    @Autowired
    ProductService productService;
    @Autowired
    UserService userService;
    @Autowired
    HttpSession session;
    @RequestMapping("/pets")
    public String selectAllpets(String petname,String pettypename,
                                Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Pet> petWrapper = new QueryWrapper<>();
        petWrapper.eq("isadopt",0);
        if(!StrUtil.isBlank(petname)){
            petWrapper.like("petname", petname);
        }
        if(!StrUtil.isBlank(pettypename)){
            petWrapper .like("pettypename", pettypename);
        }
        Page<Pet> petPage = new Page<>(page, size);
        IPage<Pet> petIPage = petService.page(petPage,petWrapper);
        String s = objectMapper.writeValueAsString(petIPage);
        return s;
    }
    @RequestMapping("/product")
    public String selectAll(String pname,
                            Integer page,Integer size) throws JsonProcessingException {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if(!StrUtil.isBlank(pname)){
            wrapper.like("pname", pname).eq("status",1);
        }
        Page<Product> page1 = new Page<>(page, size);
        IPage<Product> petIPage = productService.page(page1,wrapper);
        String s = objectMapper.writeValueAsString(petIPage);
        return s;
    }
    @PostMapping ("/login")
    public ResResult login(@RequestBody User user){
        User user1 = userService.login(user);
        if(user1==null){
            return ResResult.error("没有该用户");
        }
        UserVo userVo = new UserVo();
        userVo.setUsername(user1.getUsername());
        session.setAttribute("username",user.getUsername());
        session.setAttribute("uid",user.getUid());
        String token = JWTUtils.getToken(user1.getUid().toString(), user1.getPassword());
        userVo.setToken(token);
        userVo.setType(user1.getType());
        return ResResult.info(ResStatus.SUCCESS,"登录成功",userVo);
    }

    @PostMapping("/register")
    public ResResult regiseter(@RequestBody User user){
        user.setType("用户");
        boolean register = userService.register(user);
        if(register==true){
            return ResResult.info(ResStatus.SUCCESS,"注册成功",user);
        }
      else {
          return ResResult.info(ResStatus.FAIL,"注册失败");
        }
    }

    @GetMapping("/check")
    public ResResult countByUsername(@RequestParam String username) {
        long count = userService.count(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if(count!=0){
            return ResResult.error("用户名已存在");
        }
        return ResResult.success(null);
    }

}


